package com.example.yue.zxinglibrarytest;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;

import com.google.zxing.BarcodeFormat;

import banding.com.google.Zxing.utils.BarcodeUtils;

public class GenCodeActivity extends AppCompatActivity implements OnClickListener {

    private EditText etCharsetGencode;
    private EditText etWidthGencode;
    private EditText etHeightGencode;
    private Spinner sp1dFormatGencode;
    private Spinner sp2dFormatGencode;
    private EditText etContentGencode;
    private Button btnCreateGencode;
    private ScrollView scrollView2;
    private ImageView ivOneCodeGencode;
    private ImageView ivTwoCodeGencode;
    private TextView tvErrorGencode;
    private Bitmap bitmap2d;
    private Bitmap bitmap1d;

    private void assignViews() {
        etCharsetGencode = (EditText) findViewById(R.id.et_charset_gencode);
        etWidthGencode = (EditText) findViewById(R.id.et_width_gencode);
        etHeightGencode = (EditText) findViewById(R.id.et_height_gencode);
        sp1dFormatGencode = (Spinner) findViewById(R.id.sp_1dFormat_gencode);
        sp2dFormatGencode = (Spinner) findViewById(R.id.sp_2dFormat_gencode);
        etContentGencode = (EditText) findViewById(R.id.et_content_gencode);
        btnCreateGencode = (Button) findViewById(R.id.btn_create_gencode);
        scrollView2 = (ScrollView) findViewById(R.id.scrollView2);
        ivOneCodeGencode = (ImageView) findViewById(R.id.iv_oneCode_gencode);
        ivTwoCodeGencode = (ImageView) findViewById(R.id.iv_twoCode_gencode);
        tvErrorGencode = (TextView) findViewById(R.id.tv_error_gencode);

        btnCreateGencode.setOnClickListener(this);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gen_code);
        assignViews();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_create_gencode:
                genCodeView();
                break;
        }
    }

    private void genCodeView() {

        ivOneCodeGencode.setImageResource(0);
        ivTwoCodeGencode.setImageResource(0);
        String widthStr = etWidthGencode.getText().toString();
        String heightStr = etHeightGencode.getText().toString();

        int width = 600;
        int height = 300;
        try {
            width = Integer.parseInt(widthStr);
            height = Integer.parseInt(heightStr);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        int wh = width > height ? width : height;

        String contentText = etContentGencode.getText().toString();
        String codeCharset = etCharsetGencode.getText().toString();

        String format1dStr = sp1dFormatGencode.getSelectedItem().toString();
        String format2dStr = sp2dFormatGencode.getSelectedItem().toString();

        BarcodeFormat format1d = getFormat1d(format1dStr);
        BarcodeFormat format2d = getFormat2d(format2dStr);

        Log.d(">>>", width + "--" + height + " " + format1d.toString() + "  " + format2d.toString());

        //一二维码因编码方式不同，可能生成失败。
        try {
            bitmap2d = BarcodeUtils.createBarcode(contentText, codeCharset,wh, wh, format2d, Color.BLACK, Color.YELLOW);
            ivTwoCodeGencode.setImageBitmap(bitmap2d);

//          bitmap1d = BarcodeUtils.createBarcode(contentText, width, height, format1d, null,Color.BLACK, 0xffffccff);

            bitmap1d = BarcodeUtils.createBarcode(contentText,codeCharset,width,height,format1d,Color.BLACK,0xffffccff);
            ivOneCodeGencode.setImageBitmap(bitmap1d);

            tvErrorGencode.setText("");
        } catch (Exception e) {
            e.printStackTrace();
            tvErrorGencode.setText(e + "");
        }

    }

    private BarcodeFormat getFormat2d(String format2dStr) {
        if (format2dStr.equals(getString(R.string.QR_CODE))) {
            return BarcodeFormat.QR_CODE;
        } else if (format2dStr.equals(getString(R.string.AZTEC))) {
            return BarcodeFormat.AZTEC;
        } else if (format2dStr.equals(getString(R.string.DATA_MATRIX))) {
            return BarcodeFormat.DATA_MATRIX;
        } else if (format2dStr.equals(getString(R.string.PDF_417))) {
            return BarcodeFormat.PDF_417;
        }
        return null;
    }

    private BarcodeFormat getFormat1d(String format1dStr) {
        if (format1dStr.equals(getString(R.string.CODE_128))) {
            return BarcodeFormat.CODE_128;
        } else if (format1dStr.equals(getString(R.string.CODABAR))) {
            return BarcodeFormat.CODABAR;
        } else if (format1dStr.equals(getString(R.string.CODE_93))) {
            return BarcodeFormat.CODE_93;
        } else if (format1dStr.equals(getString(R.string.CODE_39))) {
            return BarcodeFormat.CODE_39;
        } else if (format1dStr.equals(getString(R.string.EAN_8))) {
            return BarcodeFormat.EAN_8;
        } else if (format1dStr.equals(getString(R.string.EAN_13))) {
            return BarcodeFormat.EAN_13;
        } else if (format1dStr.equals(getString(R.string.ITF))) {
            return BarcodeFormat.ITF;
        } else if (format1dStr.equals(getString(R.string.UPC_A))) {
            return BarcodeFormat.UPC_A;
        } else if (format1dStr.equals(getString(R.string.UPC_E))) {
            return BarcodeFormat.UPC_E;
        } else if (format1dStr.equals(getString(R.string.RSS_EXPANDED))) {
            return BarcodeFormat.RSS_EXPANDED;
        } else if (format1dStr.equals(getString(R.string.UPC_EAN_EXTENSION))) {
            return BarcodeFormat.UPC_EAN_EXTENSION;
        } else if (format1dStr.equals(getString(R.string.RSS_14))) {
            return BarcodeFormat.RSS_14;
        }
        return null;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        recycleBarBitmap();
    }

    private void recycleBarBitmap() {
        if (bitmap2d != null && !bitmap2d.isRecycled()) {
            bitmap2d.recycle();
            bitmap2d = null;
        }
        if (bitmap1d != null && !bitmap1d.isRecycled()) {
            bitmap1d.recycle();
            bitmap1d = null;
        }
    }
}
